/*************************************************************************
	> File Name: EP92.cpp
	> Author: 
	> Mail: 
	> Created Time: 
 ************************************************************************/
#include <stdio.h>
#define MAX_N 10000000
#define MAX_M 100000

int keep[MAX_N + 5] = {0};

int calc_n(int n) {
    int ans = 0;
    while (n) {
        int temp = n % 10;
        ans += (temp * temp);
        n /= 10;
    }
    return ans;
}

int is_val(int n) {
    if (n == 1 || n == 89) return n;
    if (n < MAX_M && keep[n]) return keep[n];
    int ret = is_val(calc_n(n));
    if (n < MAX_M) keep[n] = ret;
    return ret;
}

int main() {
    int cnt = 0;
    for (int i = 1; i < MAX_N; i++) {
        cnt += (is_val(i) == 89);
    }
    printf("%d\n", cnt);
    return 0;
}
